package com.shrct.businessprocess.purchase.impl;

import com.google.common.base.Objects;
import com.shrct.businessprocess.budget.RCBudget;
import com.shrct.businessprocess.budget.RCCostBreakDown;
import com.shrct.businessprocess.organization.RCEmployee;
import com.shrct.businessprocess.organization.RCPurchaseLeaderRoleProvider;
import com.shrct.businessprocess.organization.impl.RCEmployeeImpl;
import com.shrct.businessprocess.project.RCParameterService;
import com.shrct.businessprocess.project.RCProject;
import com.shrct.businessprocess.project.RCProjectMoney;
import com.shrct.businessprocess.project.RCProjectProcessEvent;
import com.shrct.businessprocess.project.RCProjectProcessEventStatus;
import com.shrct.businessprocess.project.RCProjectStat;
import com.shrct.businessprocess.project.impl.RCProjectProcessEventImpl;
import com.shrct.businessprocess.purchase.RCBasePurchaseOrder;
import com.shrct.businessprocess.purchase.RCBasePurchaseRequest;
import com.shrct.businessprocess.purchase.RCHasPurchaseBudget;
import com.shrct.businessprocess.purchase.RCPurchaseOrderProcess;
import com.shrct.businessprocess.purchase.impl.RCBasePurchaseOrderImpl;
import com.shrct.businessprocess.purchase.proxy.RCPurchaseOrderProcessProxy;
import com.uniquesoft.uidl.validation.Validates;
import edu.fudan.langlab.domain.organization.Party;
import edu.fudan.langlab.domain.security.ISystemUserService;
import edu.fudan.langlab.domain.workflow.ActivityNodeDef;
import edu.fudan.langlab.domain.workflow.Token;
import edu.fudan.langlab.domain.workflow.impl.BusinessProcessImpl;
import edu.fudan.mylang.common.StringExtensions;
import edu.fudan.mylang.pf.IGenericQuery;
import edu.fudan.mylang.pf.IObjectFactory;
import edu.fudan.mylang.utils.DateUtil;
import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.springframework.beans.factory.BeanFactory;

@Entity
@DiscriminatorValue("RCPURCHASEORDERPROCESS_TYPE")
public class RCPurchaseOrderProcessImpl extends BusinessProcessImpl implements RCPurchaseOrderProcess {
  public RCPurchaseOrderProcessImpl() {
    super();
  }
  
  public String getProcessName() {
    return "采购订单审批流程";
  }
  
  public String getProcessType() {
    return "RCPurchaseOrderProcess";
  }
  
  public ActivityNodeDef getActivityDef(final String activityId) {
    edu.fudan.langlab.domain.workflow.ActivityNodeDef result = new edu.fudan.langlab.domain.workflow.ActivityNodeDef();
    result.setActivityId(activityId);
    if("__end__".equals(activityId)){
    	result.setDescription("end");
    }else
    	result.setDescription(Enum.valueOf(com.shrct.businessprocess.purchase.RCPurchaseOrderProcessStatus.class, activityId).toString());
    return result;
    
  }
  
  public void _start(final RCBasePurchaseOrder purchaseOrder, final Token token) {
    BeanFactory _beanFactory = this.getBeanFactory();
    ISystemUserService _bean = _beanFactory.<ISystemUserService>getBean(ISystemUserService.class);
    Party _currentUserAsParty = _bean.getCurrentUserAsParty();
    this.starter = ((RCEmployee) _currentUserAsParty);
    this.purchaseOrder = purchaseOrder;
    purchaseOrder.startAudit();
    RCBudget _budget = purchaseOrder.getBudget();
    RCCostBreakDown _cbd = _budget.getCbd();
    RCProject _project = _cbd.getProject();RCProjectProcessEvent rCProjectProcessEvent=RCProjectProcessEventImpl.create(_project,getObjectFactory());
    final Procedure1<RCProjectProcessEvent> _function = new Procedure1<RCProjectProcessEvent>() {
        public void apply(final RCProjectProcessEvent it) {
          it.setBp(RCPurchaseOrderProcessImpl.this);
          it.setDescription("\u91C7\u8D2D\u8BA2\u5355\u5BA1\u6279,\u8BA2\u5355\u91D1\u989D:");
          RCProjectMoney _totalCost = purchaseOrder.getTotalCost();
          _totalCost.toString();
          it.setStatus(RCProjectProcessEventStatus.executing);
          Date _day = DateUtil.today();
          it.setDate(_day);
        }
      };
    ObjectExtensions.<RCProjectProcessEvent>operator_doubleArrow(rCProjectProcessEvent, _function);
  }
  
  public void start(final RCBasePurchaseOrder purchaseOrder) {
    edu.fudan.langlab.domain.workflow.Token token = getProcessManager().start(this,com.shrct.businessprocess.purchase.RCPurchaseOrderProcessStatus.PurchaseManagerAudit.name());
    _start(purchaseOrder,token);
    getProcessManager().registTask(this, token, purchaseManager(),getPurchaseManagerAudit_task_message());
    systemExecute();
    String msg = getStart_log_message(purchaseOrder);
    getProcessManager().createActivity(this,"Create Process",msg);
    
    
  }
  
  public String getStart_log_message(final RCBasePurchaseOrder purchaseOrder) {
    return "\u542F\u52A8\u91C7\u8D2D\u8BA2\u5355\u5BA1\u6279\u6D41\u7A0B";
  }
  
  public void BusinessManModify() {
  }
  
  public void _BusinessManModify_cancel(final String comment, final Token token) {
    this.purchaseOrder.auditCanceled();
    IGenericQuery<RCProjectProcessEvent> query=getObjectFactory().createGenericQuery(RCProjectProcessEvent.class,"select e from com.shrct.businessprocess.project.impl.RCProjectProcessEventImpl as e   where ((e.bp) = (:this)) and  e.active = 1      ").setParameter("this",this);
    RCProjectProcessEvent p = query.uniqueResult();
    final Procedure1<RCProjectProcessEvent> _function = new Procedure1<RCProjectProcessEvent>() {
        public void apply(final RCProjectProcessEvent it) {
          it.setDescription("\u5546\u52A1\u53D6\u6D88\u91C7\u8D2D\u7533\u8BF7");
          Date _day = DateUtil.today();
          it.setFinishDate(_day);
          it.setStatus(RCProjectProcessEventStatus.finished);
        }
      };
    ObjectExtensions.<RCProjectProcessEvent>operator_doubleArrow(p, _function);
    Token __end___token = getProcessManager().forwardTo(token,"__end__");
    
  }
  
  public void BusinessManModify_cancel(final String comment) {
    edu.fudan.langlab.domain.workflow.Token token = getProcessManager().assertToken(this,com.shrct.businessprocess.purchase.RCPurchaseOrderProcessStatus.BusinessManModify.name());
    _BusinessManModify_cancel(comment,token);
    systemExecute();
    String msg = getBusinessManModify_Cancel_log_message(comment);
    getProcessManager().createActivity(this,"商务修改订单.商务取消采购订单申请",msg);
    
    
  }
  
  public String getBusinessManModify_Cancel_log_message(final String comment) {
    String _nullsafe = StringExtensions.nullsafe(comment);
    String _plus = ("\u5546\u52A1\u53D6\u6D88\u91C7\u8D2D\u7533\u8BF7,\u7406\u7531:" + _nullsafe);
    return _plus;
  }
  
  public void _BusinessManModify_modified(final String comment, final Token token) {
    Token PurchaseManagerAudit_token = getProcessManager().forwardTo(token,com.shrct.businessprocess.purchase.RCPurchaseOrderProcessStatus.PurchaseManagerAudit.name());
    getProcessManager().registTask(this, PurchaseManagerAudit_token,purchaseManager(),getPurchaseManagerAudit_task_message());
  }
  
  public void BusinessManModify_modified(final String comment) {
    edu.fudan.langlab.domain.workflow.Token token = getProcessManager().assertToken(this,com.shrct.businessprocess.purchase.RCPurchaseOrderProcessStatus.BusinessManModify.name());
    _BusinessManModify_modified(comment,token);
    systemExecute();
    String msg = getBusinessManModify_Modified_log_message(comment);
    getProcessManager().createActivity(this,"商务修改订单.商务完成订单修改",msg);
    
    
  }
  
  public String getBusinessManModify_Modified_log_message(final String comment) {
    String _nullsafe = StringExtensions.nullsafe(comment);
    String _plus = ("\u5546\u52A1\u5B8C\u6210\u8BA2\u5355\u4FEE\u6539,\u5907\u6CE8:" + _nullsafe);
    return _plus;
  }
  
  public String getBusinessManModify_task_message() {
    RCBasePurchaseRequest _purchaseRequest = this.purchaseOrder.getPurchaseRequest();
    RCHasPurchaseBudget _budget = _purchaseRequest.getBudget();
    RCCostBreakDown _cbd = _budget.getCbd();
    RCProject _project = _cbd.getProject();
    String _code = _project.getCode();
    String _plus = ("\u9879\u76EE\u7F16\u53F7\uFF1A" + _code);
    return _plus;
  }
  
  public void PurchaseManagerAudit() {
  }
  
  public void _PurchaseManagerAudit_reject(final String comment, final Token token) {
    Token BusinessManModify_token = getProcessManager().forwardTo(token,com.shrct.businessprocess.purchase.RCPurchaseOrderProcessStatus.BusinessManModify.name());
    getProcessManager().registTask(this, BusinessManModify_token,businessMan(),getBusinessManModify_task_message());
  }
  
  public void PurchaseManagerAudit_reject(final String comment) {
    edu.fudan.langlab.domain.workflow.Token token = getProcessManager().assertToken(this,com.shrct.businessprocess.purchase.RCPurchaseOrderProcessStatus.PurchaseManagerAudit.name());
    _PurchaseManagerAudit_reject(comment,token);
    systemExecute();
    String msg = getPurchaseManagerAudit_Reject_log_message(comment);
    getProcessManager().createActivity(this,"采购经理审批.总经理审批助理理拒绝",msg);
    
    
  }
  
  public String getPurchaseManagerAudit_Reject_log_message(final String comment) {
    String _nullsafe = StringExtensions.nullsafe(comment);
    String _plus = ("\u603B\u7ECF\u7406\u5BA1\u6279\u52A9\u7406\u62D2\u7EDD,\u7406\u7531:" + _nullsafe);
    return _plus;
  }
  
  public void _PurchaseManagerAudit_agree(final String comment, final Token token) {
    this.purchaseOrder.auditPassed();
    BeanFactory _beanFactory = this.getBeanFactory();
    RCParameterService _bean = _beanFactory.<RCParameterService>getBean(RCParameterService.class);
    String _nextOrderId = _bean.getNextOrderId();
    this.purchaseOrder.setOrderNumber(_nextOrderId);
    IGenericQuery<RCProjectProcessEvent> query=getObjectFactory().createGenericQuery(RCProjectProcessEvent.class,"select e from com.shrct.businessprocess.project.impl.RCProjectProcessEventImpl as e   where ((e.bp) = (:this)) and  e.active = 1      ").setParameter("this",this);
    RCProjectProcessEvent p = query.uniqueResult();
    final Procedure1<RCProjectProcessEvent> _function = new Procedure1<RCProjectProcessEvent>() {
        public void apply(final RCProjectProcessEvent it) {
          RCProjectMoney _totalCost = RCPurchaseOrderProcessImpl.this.purchaseOrder.getTotalCost();
          String _string = _totalCost.toString();
          String _plus = ("\u91C7\u8D2D\u7533\u8BF7\u5BA1\u6279\u901A\u8FC7,\u91C7\u8D2D\u8BA2\u5355\u91D1\u989D:" + _string);
          it.setDescription(_plus);
          Date _day = DateUtil.today();
          it.setFinishDate(_day);
          it.setStatus(RCProjectProcessEventStatus.finished);
        }
      };
    ObjectExtensions.<RCProjectProcessEvent>operator_doubleArrow(p, _function);
    RCBudget _budget = this.purchaseOrder.getBudget();
    RCCostBreakDown _cbd = _budget.getCbd();
    RCProject _project = _cbd.getProject();
    RCProjectStat _stat = _project.getStat();
    _stat.updateInfo();
    RCBudget _budget_1 = this.purchaseOrder.getBudget();
    RCCostBreakDown _cbd_1 = _budget_1.getCbd();
    RCProject _project_1 = _cbd_1.getProject();
    _project_1.setActualCostDirtyFlag(Boolean.valueOf(true));
    Token __end___token = getProcessManager().forwardTo(token,"__end__");
    
  }
  
  public void PurchaseManagerAudit_agree(final String comment) {
    edu.fudan.langlab.domain.workflow.Token token = getProcessManager().assertToken(this,com.shrct.businessprocess.purchase.RCPurchaseOrderProcessStatus.PurchaseManagerAudit.name());
    _PurchaseManagerAudit_agree(comment,token);
    systemExecute();
    String msg = getPurchaseManagerAudit_Agree_log_message(comment);
    getProcessManager().createActivity(this,"采购经理审批.总经理审批助理同意",msg);
    
    
  }
  
  public String getPurchaseManagerAudit_Agree_log_message(final String comment) {
    String _nullsafe = StringExtensions.nullsafe(comment);
    String _plus = ("\u603B\u7ECF\u7406\u5BA1\u6279\u52A9\u7406\u540C\u610F\u8BA2\u5355\u91C7\u8D2D,\u5907\u6CE8:" + _nullsafe);
    return _plus;
  }
  
  public String getPurchaseManagerAudit_task_message() {
    RCBasePurchaseRequest _purchaseRequest = this.purchaseOrder.getPurchaseRequest();
    RCHasPurchaseBudget _budget = _purchaseRequest.getBudget();
    RCCostBreakDown _cbd = _budget.getCbd();
    RCProject _project = _cbd.getProject();
    String _code = _project.getCode();
    String _plus = ("\u9879\u76EE\u7F16\u53F7\uFF1A" + _code);
    return _plus;
  }
  
  @JoinColumn(name = "purchaseOrder_id")
  @ManyToOne(targetEntity = RCBasePurchaseOrderImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST })
  private RCBasePurchaseOrder purchaseOrder;
  
  public RCBasePurchaseOrder getPurchaseOrder() {
    return this.purchaseOrder;
  }
  
  public RCPurchaseOrderProcess setPurchaseOrder(final RCBasePurchaseOrder purchaseOrder) {
    this.purchaseOrder = purchaseOrder;
    return this;			
    
  }
  
  @JoinColumn(name = "starter_id")
  @ManyToOne(targetEntity = RCEmployeeImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST })
  private RCEmployee starter;
  
  public RCEmployee getStarter() {
    return this.starter;
  }
  
  public RCPurchaseOrderProcess setStarter(final RCEmployee starter) {
    this.starter = starter;
    return this;			
    
  }
  
  public boolean _systemExecute() {
    boolean stateChanged = false;
    return stateChanged;
  }
  
  public static RCPurchaseOrderProcess create(final IObjectFactory objectFactory) {
    com.shrct.businessprocess.purchase.RCPurchaseOrderProcess rCPurchaseOrderProcess = new com.shrct.businessprocess.purchase.impl.RCPurchaseOrderProcessImpl();
    objectFactory.create(rCPurchaseOrderProcess);
    return rCPurchaseOrderProcess;			
    
  }
  
  public Party businessMan() {
    return this.starter;
  }
  
  public Party purchaseManager() {
    RCEmployee _xblockexpression = null;
    {
      BeanFactory _beanFactory = this.getBeanFactory();
      RCPurchaseLeaderRoleProvider _bean = _beanFactory.<RCPurchaseLeaderRoleProvider>getBean(RCPurchaseLeaderRoleProvider.class);
      final RCEmployee res = _bean.getParty();
      boolean _notEquals = (!Objects.equal(res, null));
      Validates.Assert(_notEquals, "\u672A\u8BBE\u7F6E\u91C7\u8D2D\u7ECF\u7406");
      _xblockexpression = (res);
    }
    return _xblockexpression;
  }
  
  public RCPurchaseOrderProcessProxy toProxy() {
    com.shrct.businessprocess.purchase.proxy.RCPurchaseOrderProcessProxy proxy = new com.shrct.businessprocess.purchase.proxy.RCPurchaseOrderProcessProxy();
    proxy.setId(this.getId());
    if(this instanceof edu.fudan.mylang.pf.INamedModelObject){
    	proxy.setName(((edu.fudan.mylang.pf.INamedModelObject)this).getEntityName());
    }
    return proxy;
    
  }
}
